<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>javascript type test</title>
</head>
<body>
	<script>
	var type = {
		/*它只在区别对象和原始类型的时候才有用
			typeof null得到object，
			typeof undefined得到undefined，
			typeof 数组对象得到object，
			typeof 函数得到function
		*/
		simpleType : ((function(){
			console.info('typeof {} -->' + typeof {});  //object
			console.info('typeof [] -->' + typeof []);  //object
			console.info('typeof new Date -->' + typeof new Date);  //object
		})(),
		
		ObjectType : function(){
			[] instanceof Array; // true 
			[] instanceof Object; // true 
			[] instanceof RegExp; // false 
			new Date() instanceof Date; // true 
		},
		
		isArray : function (arr){ 
			return typeof arr == "object" && arr.constructor == Array; 
		},
		
		isArray : function(arr){ 
			return Object.prototype.toString.call(arr) === "[object Array]"; 
		},
		
		/*跨原型链调用toString(), 得到国外多个javaScript大师的认可 */
		test : function(){
			Object.prototype.toString.call([]); // 返回 "[object Array]" 
			Object.prototype.toString.call(/reg/ig); // 返回 "[object RegExp]" 
			
	        var d=new Date();
	    	alert(Object.prototype.toString.apply(d));//[object Date]
	    	var a=[];
	    	alert(Object.prototype.toString.apply(a));//[object Array]
		}
		
		/*详细出处参考：http://www.jb51.net/article/21109.htm*/
	}

	function getType(x){
		if(x==null){
			return "null";
		}
		var t= typeof x;
		if(t!="object"){
			return t;
		}
		var c=Object.prototype.toString.apply(x);
		c=c.substring(8,c.length-1);
		if(c!="Object"){
			return c;
		}
		if(x.constructor==Object){
			return c
		}
		if("classname" in x.prototype.constructor
				&& typeof x.prototype.constructor.classname=="string"){
			return x.constructor.prototype.classname;
		}
		return "<unknown type>";
	}

    function strReplaceTest(){
		// Mistake
		// 踩到坑了
		var str = "David is an Arsenal fan, which means David is great";
		str.replace("David", "Darren"); // "Darren is an Arsenal fan, which means David is great"
	
		// Desired
		// 符合预期
		str.replace(/David/g, "Darren"); // "Darren is an Arsenal fan, which means Darren is great"
    }

    function prototypeSliceTest(){
		// "true" array of DIVs
		// 得到一个由 div 元素组成的“真正的”数组
		//querySelector和querySelectorAll 是html5 新增
		//http://www.w3.org/TR/selectors-api/#nodeselector
	    var nodesArr = Array.prototype.slice.call(document.querySelectorAll("div"));
	
		 // changes arguments to "true" array
		 // 把 arguments 转换成一个“真正的”数组
		 var argsArr = Array.prototype.slice.call(arguments);
	
		 var clone = myArray.slice(0); //浅克隆一个数组

    }

    function sort(){
    	[1, 3, 9, 2].sort();
        // Returns: [1, 2, 3, 9]
        // 返回 [1, 2, 3, 9]

    	[
    	    { name: "Robin Van PurseStrings", age: 30 },
    	    { name: "Theo Walcott", age: 24 },
    	    { name: "Bacary Sagna", age: 28  }
    	].sort(function(obj1, obj2) {
    	    // Ascending: first age less than the previous
    	    // 实现增序排列：前者的 age 小于后者
    	    return obj1.age - obj2.age;
    	});
    }

    function arrayMerge(){
    	var mergeTo = [4,5,6];
    	var mergeFrom = [7,8,9];
    	mergeTo = mergeTo.concat(mergeFrom);   //产生合并副本, 不会改变现有的数组
    	//mergeTo; // is: [4, 5, 6, 7, 8, 9]

		var mergeTo = [4,5,6];
		var mergeFrom = [7,8,9];
		Array.prototype.push.apply(mergeTo, mergeFrom);  //
		//mergeTo; // is: [4, 5, 6, 7, 8, 9]    	
    }
	</script>
</body>
</html>